package gold.digger;

import java.util.*;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC1249 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public String minRemoveToMakeValid(String s) {
            StringBuilder res = new StringBuilder();
            Stack<Character> st = new Stack<>();
            Stack<Integer> posSt = new Stack<>();

            for (char ch : s.toCharArray()) {
                if (Character.isLowerCase(ch)) res.append(ch);
                else if ('(' == ch) {
                    st.push(ch);
                    posSt.push(res.length());
                    res.append(ch);
                } else {
                    //当前是）
                    if (st.isEmpty()) continue;
                    st.pop();
                    posSt.pop();
                    res.append(ch);
                }
            }

            while (!posSt.isEmpty()) {
                res.deleteCharAt(posSt.pop());
            }

            return res.toString();
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.minRemoveToMakeValid("(a(b(c)d)"));

//        StringBuilder res = new StringBuilder("0123456");
//        Stack<Integer> posSt = new Stack<>();
//        posSt.push(5);
//        posSt.push(1);
//        while (!posSt.isEmpty()) {
//            res.deleteCharAt(posSt.pop());
//        }
    }

    public static void main(String[] args) throws Exception {
        LC1249 an = new LC1249();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
